package com.workerhelping.listener;

import com.alibaba.fastjson.JSON;
import com.workerhelping.constant.MQConstant;
import com.workerhelping.dto.ESAssistInfoDTO;
import com.workerhelping.elasticsearchEntity.ESAssistVO;
import com.workerhelping.elasticsearchEntity.ESIncomeVO;
import com.workerhelping.elasticsearchEntity.ESRecordVO;
import com.workerhelping.feiClients.ESFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.channels.Channel;
import java.util.List;

@Slf4j
@Component
public class MessageListener {

    @Autowired
    private ESFeignClient esFeignClient;


    /**
     * 监听删除档案队列
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstant.QUEUE_RECORD_DELETE, durable = "true"),
            exchange = @Exchange(value = MQConstant.EXCHANGE_RECORD),
            key = MQConstant.ROUTINGKEY_RECORD_DELETE
    ))
    public void deleteRecord(Message message) {
        String messageBody = new String(message.getBody());
        ESRecordVO esRecordVO = JSON.parseObject(messageBody, ESRecordVO.class);
        esFeignClient.deleteRecord(esRecordVO);
    }

    /**
     * 监听插入或更新档案队列
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstant.QUEUE_RECORD_INSERTORUPDATE, durable = "true"),
            exchange = @Exchange(value = MQConstant.EXCHANGE_RECORD),
            key = MQConstant.ROUTINGKEY_RECORD_INSERTORUPDATE
    ))
    public void insertOrUpdateRecord(Message message) {
        String messageBody = new String(message.getBody());
        ESRecordVO esRecordVO = JSON.parseObject(messageBody, ESRecordVO.class);
        esFeignClient.insertOrUpdateRecord(esRecordVO);
    }

    /**
     * 监听收入信息队列
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstant.QUEUE_INCOME_INSERT, durable = "true"),
            exchange = @Exchange(value = MQConstant.EXCHANGE_INCOME),
            key = MQConstant.ROUTINGKEY_INCOME_INSERT
    ))
    public void insertIncome(Message message) {
        String messageBody = new String(message.getBody());
        ESIncomeVO esIncomeVO = JSON.parseObject(messageBody, ESIncomeVO.class);
        esFeignClient.insertIncome(esIncomeVO);
    }

    /**
     * 监听补助信息信息队列
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstant.QUEUE_ASSIST_INSERT, durable = "true"),
            exchange = @Exchange(value = MQConstant.EXCHANGE_ASSIST),
            key = MQConstant.ROUTINGKEY_ASSIST_INSERT
    ))
    public void insertAssist(Message message) {
        String messageBody = new String(message.getBody());
        List<ESAssistVO> assistVOS = JSON.parseArray(messageBody, ESAssistVO.class);
        esFeignClient.insertAssistBatch(new ESAssistInfoDTO(assistVOS));
    }
}
